﻿using System;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using WAD.Model;
using System.Text;
using System.Collections.Generic;

namespace WAD.DAL
{
    public class DAOFCSetting : DAO
    {
        public static bool Add(MFCSetting m)
        {
            String sql = "insert into fc_setting([user_id],truename,mobile,wx_name,wx_number,wx_picurl,ali_name,ali_truename,state,date,area1,area2,area3)values(@user_id,@truename,@mobile,@wx_name,@wx_number,@wx_picurl,@ali_name,@ali_truename,@state,@date,@area1,@area2,@area3)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@truename", DbType.String, m.truename);
            db.AddInParameter(cmd, "@mobile", DbType.String, m.mobile);
            db.AddInParameter(cmd, "@wx_name", DbType.String, m.wx_name);
            db.AddInParameter(cmd, "@wx_number", DbType.Int32, m.wx_number);
            db.AddInParameter(cmd, "@wx_picurl", DbType.String, m.wx_picurl);
            db.AddInParameter(cmd, "@ali_name", DbType.String, m.ali_name);
            db.AddInParameter(cmd, "@ali_truename", DbType.String, m.ali_truename);
            db.AddInParameter(cmd, "@state", DbType.Int32, m.state);
            db.AddInParameter(cmd, "@date", DbType.String, m.date.Value);
            db.AddInParameter(cmd, "@area1", DbType.String, m.area1);
            db.AddInParameter(cmd, "@area2", DbType.String, m.area2);
            db.AddInParameter(cmd, "@area3", DbType.String, m.area3);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        static int Add(int user_id, int state, string area1, string area2, string area3)
        {
            String sql = "insert into fc_setting([user_id],state,date,area1,area2,area3)values(@user_id,@state,getdate(),@area1,@area2,@area3)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, user_id);
            db.AddInParameter(cmd, "@state", DbType.Int32, state);
            db.AddInParameter(cmd, "@area1", DbType.String, area1);
            db.AddInParameter(cmd, "@area2", DbType.String, area2);
            db.AddInParameter(cmd, "@area3", DbType.String, area3);
            return db.ExecuteNonQuery(cmd);
        }

        public static bool UpdateInfo(MFCSetting m)
        {
            if (m.user_id < 1) return false;

            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(m.truename))
                sb.AppendFormat(",truename='{0}'", m.truename);
            if (!string.IsNullOrEmpty(m.mobile))
                sb.AppendFormat(",mobile='{0}'", m.mobile);
            if (!string.IsNullOrEmpty(m.wx_name))
                sb.AppendFormat(",wx_name='{0}'", m.wx_name);
            if (!string.IsNullOrEmpty(m.wx_number))
                sb.AppendFormat(",wx_number='{0}'", m.wx_number);
            if (!string.IsNullOrEmpty(m.wx_picurl))
                sb.AppendFormat(",wx_picurl='{0}'", m.wx_picurl);

            if (!string.IsNullOrEmpty(m.ali_name))
                sb.AppendFormat(",ali_name='{0}'", m.ali_name);

            if (!string.IsNullOrEmpty(m.ali_truename))
                sb.AppendFormat(",ali_truename='{0}'", m.ali_truename);

            if (!string.IsNullOrEmpty(m.area1))
            {
                sb.AppendFormat(",area1='{0}'", m.area1);
            }
            if (!string.IsNullOrEmpty(m.area2))
            {
                sb.AppendFormat(",area2='{0}'", m.area2);
            }
            if (!string.IsNullOrEmpty(m.area3))
            {
                sb.AppendFormat(",area3='{0}'", m.area3);
            }

            if (sb.Length > 0)
            {
                sb.Insert(0, "state=0");
                return ExecuteNonQuery(string.Format("update fc_setting set {0} where user_id={1}", sb.ToString(), m.user_id)) > 0;
            }
            return false;
        }

        /// <summary>
        /// 更新自己的定位
        /// </summary>
        /// <param name="user_id"></param>
        /// <param name="area1"></param>
        /// <param name="area2"></param>
        /// <param name="area3"></param>
        /// <returns></returns>
        public static int UpdateLocation(int user_id, string area1, string area2, string area3)
        {
            if (user_id < 1) return 0;
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(area1))
                sb.AppendFormat(",area1='{0}'", area1);

            if (!string.IsNullOrEmpty(area2))
                sb.AppendFormat(",area2='{0}'", area2);

            if (!string.IsNullOrEmpty(area3))
                sb.AppendFormat(",area3='{0}'", area3);

            if (sb.Length < 1) return 0;
            
            int count = 0;
            sb.Remove(0, 1);
            try
            {
                count = ExecuteNonQuery(string.Format("update fc_setting set {0} where user_id={1}", sb.ToString(), user_id));
                if (count < 1 && user_id > 0)
                    count = Add(user_id, 0, area1, area2, area3);
            }
            catch (Exception ex) { DaoLogcat.Add("location错误", ex.Message + string.Format("update fc_setting set {0} where user_id={1}", sb.ToString(), user_id)); }
            return count;
        }

        public static object Exists(string where, string field = "")
        {
            if (string.IsNullOrEmpty(field))
                field = "id";
            if (!string.IsNullOrEmpty(where))
                return ExecuteScalar(string.Format("select top 1 {0} from fc_setting where {1}", field, where));
            else
                return ExecuteScalar(string.Format("select top 1 {0} from fc_setting where {1}", field, where));
        }

        /// <summary>
        /// 管理后台：审核分享者
        /// </summary>
        /// <param name="state"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static int Check(int state, string ids)
        {
            return ExecuteNonQuery(string.Format("update fc_setting set state={0} where id in({1})", state, ids));
        }

        public static PageData GetListPage(MFCSetting m, int pageIndex, int pageSize, string fieldList = "*")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("1=1");
            if (m.user_id > 0)
                sb.AppendFormat(" and user_id={0}", m.user_id);
            if (m.state > -1)
                sb.AppendFormat(" and state={0}", m.state);

            return GetPageList(fieldList, "id desc", pageIndex, pageSize, sb.ToString(), "fc_setting");
        }

        /// <summary>
        ///  获取推送的channelID列表
        /// </summary>
        /// <param name="area1"></param>
        /// <param name="area2"></param>
        /// <param name="area3"></param>
        /// <returns></returns>
        public static List<Dictionary<string, object>> GetListPush(object area1, object area2, object area3)
        {
            StringBuilder sb = new StringBuilder();
            if (area3 != null && area3 != DBNull.Value)
                sb.AppendFormat(" and isnull(area3,'{0}')='{0}'", area3);   //区
            else if (area2 != null && area2 != DBNull.Value)
                sb.AppendFormat(" and isnull(area2,'{0}')='{0}'", area2);   //市
            else if (area1 != null && area1 != DBNull.Value)
                sb.AppendFormat(" and isnull(area1,'{0}')='{0}'", area1);   //省

            //日志
            DaoLogcat.Add("push", "where: p.user_id=s.user_id and p.channelid !='' and area1!=''" + sb.ToString());

            //return GetList("select p.userid,p.channelid,clientid,store,s.area1,s.area2,s.area3 from push p,fc_setting s where p.user_id=s.user_id and p.channelid !='' and area1!=''" + sb.ToString() + " order by s.user_id desc");
            return GetList("select p.userid,p.channelid,clientid,store from push p,fc_setting s where p.user_id=s.user_id and p.channelid !='' and area1!=''" + sb.ToString() + " order by s.user_id desc");
        }

        public static List<Dictionary<string, object>> GetListPush(object area1, object area2, object area3,string ids)
        {
            StringBuilder sb = new StringBuilder();
            if (area3 != null && area3 != DBNull.Value && area3.ToString() != "")
                sb.AppendFormat(" and isnull(area3,'{0}')='{0}'", area3);   //区
            else if (area2 != null && area2 != DBNull.Value && area2.ToString() !="")
                sb.AppendFormat(" and isnull(area2,'{0}')='{0}'", area2);   //市
            else if (area1 != null && area1 != DBNull.Value && area1.ToString() != "")
                sb.AppendFormat(" and isnull(area1,'{0}')='{0}'", area1);   //省

            sb.AppendFormat(" and p.user_id in({0})", ids);
            //日志
            DaoLogcat.Add("push", "where: p.user_id=s.user_id and p.channelid !='' and area1!=''" + sb.ToString());

            return GetList("select p.userid,p.channelid,clientid,store,s.area1,s.area2,s.area3 from push p,fc_setting s where p.user_id=s.user_id and p.channelid !='' and area1!=''" + sb.ToString());
        }
    }
}